package com.eric.ztea.controller;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eric.ztea.dao.CityRepository;
import com.eric.ztea.domain.City;


@RestController
@RequestMapping("/api/city")
public class CityController {

	@Autowired
    CityRepository cityRepository;	
	
	@GET
	@RequestMapping(value = "/findById")
	public City findById(@RequestParam(value = "id", required = true) Long id) {
		return cityRepository.findById(id);
	}

	@GET
	@RequestMapping(value = "/findByName")
	public City findByName(@RequestParam(value = "name", required = true) String name) {
		return cityRepository.findByName(name);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public City save(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "parentid", required = false) Long parentid) {
		return cityRepository.save(new City(id, name, parentid));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		cityRepository.delete(id);
	}
	
}
